#include "signup.h"

int signup(int lcdp[][800])
{
	FILE *pic = fopen("./res/sign.bmp", "r");
	if (pic == NULL)
	{
		printf("signup open fail\n");
		return -1;
	}
	unsigned char buf[480][800 * 3] = {0};
	fseek(pic, 54, SEEK_SET);
	fread(buf, 3, 480 * 800, pic);
	for (int i = 0; i < 480; i++)
		for (int j = 0; j < 800; j++)
		{
			int y = 479 - i;
			lcdp[y][j] = 0x00 << 24 | buf[i][j * 3 + 2] << 16 | buf[i][j * 3 + 1] << 8 | buf[i][j * 3];
		}
	int lcdt = open("/dev/input/event0", O_RDWR);
	if (lcdt < 0)
	{
		perror("input error\n");
		return -1;
	}
	struct input_event touch;
	int x = -1, y = -1;
	// 字体
	char acc[] = "账号最多10位:)";
	char key[] = "密码最多7位:(";
	char errorbuf[] = "请开始注册，点击输入框可以清空内容";
	font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
	fontSetSize(f, 50);
	bitmap *ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
	bitmap *pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
	fontPrint(f, ac, 0, 0, acc, getColor(0, 0, 0, 255), 1000);
	fontPrint(f, pa, 0, 0, key, getColor(0, 0, 255, 0), 1000);
	show_font_to_lcd(lcdp, 110, 35, ac);
	show_font_to_lcd(lcdp, 100, 140, pa);
	font *ef = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
	fontSetSize(ef, 25);
	bitmap *er = createBitmapWithInit(330, 40, 4, getColor(255, 255, 255, 255));
	fontPrint(ef, er, 0, 0, errorbuf, getColor(0, 255, 0, 0), 400);
	show_font_to_lcd(lcdp, 70, 325, er);
	char accnum[11] = {0};
	char keynum[8] = {0};
	char kyfake[8] = {0};
	int actimes = 0;
	int kytimes = 0;
	int flag = 1;
	while (1)
	{
		int read_ret = read(lcdt, &touch, sizeof(struct input_event));
		if (read_ret < 0)
		{
			printf("read touch fail\n");
			return -1;
		}
		if (touch.type == EV_ABS)
		{
			if (touch.code == ABS_X)
				x = touch.value * 800 / 1024;
			else if (touch.code == ABS_Y)
				y = touch.value * 480 / 800;
		}
		printf("x=%d y=%d\naccnum:%s keynum:%s actimes=%d kytimes=%d flag=:%d\n", x, y, accnum, keynum, actimes, kytimes, flag);

		if (touch.type == EV_KEY)
		{
			if (touch.code == BTN_TOUCH && touch.value == 1)
			{
				if ((x > 0 && x < 60) && (y > 305 && y < 480))
				{
					printf("exit signup\n");
					goto done;
				}
				// 账号输入框位置 最多10位
				if ((x > 40 && x < 440) && (y > 20 && y < 80) && acc != 0)
				{
					printf("entering account\n");
					memset(acc, 0, strlen(acc));
					memset(accnum, 0, strlen(accnum));
					actimes = 0;
					ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
					fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
					show_font_to_lcd(lcdp, 110, 35, ac);
					flag = 1;
				}
				else if ((x > 40 && x < 440) && (y > 110 && y < 160) && key != 0)
				{
					printf("entering password\n");
					memset(key, 0, strlen(key));
					memset(keynum, 0, strlen(keynum));
					memset(kyfake, 0, strlen(kyfake));
					kytimes = 0;
					pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
					fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 255, 0), 1000);
					show_font_to_lcd(lcdp, 100, 140, pa);
					flag = 0;
				}
				if (flag == 1)
				{
					if ((x > 490 && x < 530) && (y > 60 && y < 100))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 1\n");
						accnum[actimes] = '1';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 570 && x < 650) && (y > 60 && y < 100))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 2\n");
						accnum[actimes] = '2';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 690 && x < 790) && (y > 60 && y < 100))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 3\n");
						accnum[actimes] = '3';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 490 && x < 530) && (y > 150 && y < 200))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 4\n");
						accnum[actimes] = '4';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 580 && x < 650) && (y > 150 && y < 200))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 5\n");
						accnum[actimes] = '5';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 690 && x < 790) && (y > 150 && y < 200))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 6\n");
						accnum[actimes] = '6';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 490 && x < 530) && (y > 240 && y < 280))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 7\n");
						accnum[actimes] = '7';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 580 && x < 650) && (y > 240 && y < 280))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 8\n");
						accnum[actimes] = '8';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 690 && x < 790) && (y > 240 && y < 280))
					{
						if (actimes == 10)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 9\n");
						accnum[actimes] = '9';
						ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
						show_font_to_lcd(lcdp, 110, 35, ac);
						actimes++;
					}
					if ((x > 470 && x < 590) && (y > 310 && y < 480))
					{
						if (actimes != 0)
						{
							printf("reback ac\n");
							actimes--;
							accnum[actimes] = 0;
							ac = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
							fontPrint(f, ac, 0, 0, accnum, getColor(0, 0, 0, 255), 1000);
							show_font_to_lcd(lcdp, 110, 35, ac);
						}
					}
				}
				if (flag == 0)
				{
					if ((x > 490 && x < 530) && (y > 60 && y < 100))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 1\n");
						keynum[kytimes] = '1';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 570 && x < 650) && (y > 60 && y < 100))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 2\n");
						keynum[kytimes] = '2';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 690 && x < 790) && (y > 60 && y < 100))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 3\n");
						keynum[kytimes] = '3';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 490 && x < 530) && (y > 150 && y < 200))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 4\n");
						keynum[kytimes] = '4';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 570 && x < 650) && (y > 150 && y < 200))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 5\n");
						keynum[kytimes] = '5';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 690 && x < 790) && (y > 150 && y < 200))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 6\n");
						keynum[kytimes] = '6';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 490 && x < 530) && (y > 240 && y < 280))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 7\n");
						keynum[kytimes] = '7';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 570 && x < 650) && (y > 240 && y < 280))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 8\n");
						keynum[kytimes] = '8';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 690 && x < 790) && (y > 240 && y < 280))
					{
						if (kytimes == 7)
						{
							printf("输入框到达上限\n");
							continue;
						}
						printf("entering 9\n");
						keynum[kytimes] = '9';
						kyfake[kytimes] = '*';
						pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
						fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
						show_font_to_lcd(lcdp, 100, 140, pa);
						kytimes++;
					}
					if ((x > 470 && x < 590) && (y > 310 && y < 480))
					{
						if (kytimes != 0)
						{
							printf("reback ky\n");
							kytimes--;
							keynum[kytimes] = 0;
							kyfake[kytimes] = 0;
							pa = createBitmapWithInit(270, 50, 4, getColor(255, 255, 255, 255));
							fontPrint(f, pa, 0, 0, kyfake, getColor(0, 0, 255, 0), 400);
							show_font_to_lcd(lcdp, 100, 140, pa);
						}
					}
				}
				if ((x > 125 && x < 350) && (y > 300 && y < 330))
				{
					if (actimes != 0 && kytimes != 0)
					{
						FILE *ak = fopen("./lib/acckey.txt", "a+");
						char user[10 * 10]; // 最多10个用户
						int i = 0;
						fseek(ak, 0, SEEK_SET);
						while (fscanf(ak, "%s", user) != EOF)
						{
							if (strcmp(accnum, user) == 0)
							{
								flag = 1;
								break;
							}
						}
						printf("flag:%d\n", flag);
						if (flag == 0)
						{
							fprintf(ak, "%s %s\n", accnum, keynum);
							fclose(ak);
							printf("注册成功\n");
							char suc[] = "                     账号注册成功";
							er = createBitmapWithInit(330, 40, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, suc, getColor(0, 0, 0, 255), 400);
							show_font_to_lcd(lcdp, 70, 325, er);
							sleep(1);
							goto done;
						}
						else
						{
							char suc[] = "                     账号已存在:(";
							er = createBitmapWithInit(330, 40, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, suc, getColor(0, 255, 0, 0), 400);
							show_font_to_lcd(lcdp, 70, 325, er);
							sleep(1);
							goto done;
						}
					}
					else
					{
						char error[] = "                   账号或密码为空";
						printf("%s\n", errorbuf);
						er = createBitmapWithInit(330, 40, 4, getColor(255, 255, 255, 255));
						fontPrint(ef, er, 0, 0, error, getColor(0, 0, 0, 255), 400);
						show_font_to_lcd(lcdp, 70, 325, er);
					}
				}
			}
		}
	}
done:
	close(lcdt);
	fclose(pic);
	fontUnload(f);
	fontUnload(ef);
	destroyBitmap(ac);
	destroyBitmap(pa);
	destroyBitmap(er);
	return 1;
}
